package zju.dgm.service.election;

import org.apache.curator.framework.CuratorFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import zju.dgm.dto.DataFrame;
import zju.dgm.service.loadbalance.LoadBalancePath;
import zju.dgm.service.loadbalance.LoadBalanceUtilProvider;
import zju.dgm.service.region.RegionUtilProvider;

import java.util.concurrent.*;

/**
 * @author lzy
 */
@Component
public class MasterThread extends Thread {

    private final static Logger logger = LoggerFactory.getLogger(MasterThread.class);


    @Autowired
    LoadBalancePath path;

    @Autowired
    RegionUtilProvider regionUtilProvider;

    @Autowired
    LoadBalanceUtilProvider loadBalanceUtilProvider;

    @Autowired
    CuratorFramework client;

    @Autowired
    ElectionService electionService;


    private final ThreadPoolExecutor masterTpe = new ThreadPoolExecutor(
            5, Integer.MAX_VALUE, 1, TimeUnit.MINUTES,
            new LinkedBlockingDeque<>()
    );


    @Override
    public void run() {
        for(Runnable task:electionService.masterTaskList){
            logger.info("master重拾之前分配的任务");
            masterTpe.execute(task);
        }
        while (true) {
            try {
                Thread.sleep(Integer.MAX_VALUE);
            } catch (InterruptedException e) {
                e.printStackTrace();
                masterTpe.shutdown();
                return;
            }
        }
    }

    void addTask(Runnable task) {
        logger.info("master接到新任务");
        masterTpe.submit(task);
    }



}
